package com.gms.tool.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gms.tool.ShareData;



public class MysqlConnection {

	
	private static Connection getConnect() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		
		return DriverManager.getConnection("jdbc:mysql://" + ShareData.ADDRESS + "/" + ShareData.DBNAME, ShareData.DBLOGIN, ShareData.DBPASSWORD);
	}

	public static ResultSet selectUser(String username) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		
		String requestSQL = "select *from " + ShareData.USERS_TABLE + " where " + ShareData.USERS_NAME +"='" + username + "'";
		
		return getConnect().createStatement().executeQuery(requestSQL);
	}

	public static ResultSet selectmail(String mail) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		
		String requestSQL = "select *from " + ShareData.USERS_TABLE + " where " + ShareData.USERS_MAIL +"='" + mail + "'";
		
		return getConnect().createStatement().executeQuery(requestSQL);
	}

	public static boolean createUser(String username, String password, String mail) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		
		String requestSQL = "insert into users (name,password,mail,city,country) values('"+username+"','"+password+"','"+mail+"','','')";

		getConnect().createStatement().executeUpdate(requestSQL);
		
		return true;
	}
	
}
